package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * projectName: shf
 *
 * @author: cbc
 * time: 2022/6/10 14:30 周五
 * description:
 */
@Controller
@RequestMapping("/community")
public class CommunityController extends BaseController {

    private final static String LIST_ACTION = "redirect:/community";

    private final static String PAGE_INDEX = "community/index";
    private final static String PAGE_SHOW = "community/show";
    private final static String PAGE_CREATE = "community/create";
    private final static String PAGE_EDIT = "community/edit";
    private final static String PAGE_SUCCESS = "common/successPage";

    @Reference
    private CommunityService communityService;
    @Reference
    private DictService dictService;

    /**
     * 列表
     *
     * @param modelMap
     * @param request
     * @return
     */
    @RequestMapping
    public String index(ModelMap modelMap, HttpServletRequest request) {
        Map<String, Object> filters = getFilters((request));
        if (!filters.containsKey("areaId")) {
            filters.put("areaId", "");
        }
        if (!filters.containsKey("plateId")) {
            filters.put("plateId", "");
        }
        PageInfo<Community> page = communityService.findPage(filters);
        //根据市区进行查询
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        modelMap.addAttribute("areaList",areaList);
        modelMap.addAttribute("page",page);
        modelMap.addAttribute("filters",filters);
        return PAGE_INDEX;
    }

    /**
     * 进入新增
     *
     * @param modelMap
     * @return
     */
    @GetMapping("/create")
    public String create(ModelMap modelMap) {
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        modelMap.addAttribute("areaList", areaList);
        return PAGE_CREATE;
    }

    /**
     * 新增保存
     *
     * @param community
     * @return
     */
    @PostMapping("/save")
    public String save(Community community) {
        communityService.insert(community);
        return PAGE_SUCCESS;
    }

    /**
     * 编辑
     *
     * @param modelMap
     * @param id
     * @return
     */
    @GetMapping("/edit/{id}")
    public String edit(ModelMap modelMap, @PathVariable Long id) {
        Community community = communityService.getById(id);
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        modelMap.addAttribute("community", community);
        modelMap.addAttribute("areaList", areaList);
        return PAGE_EDIT;
    }

    /**
     * 保存更新
     *
     * @param community
     * @return
     */
    @PostMapping("/update")
    public String update(Community community) {
        communityService.update(community);
        return PAGE_SUCCESS;
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        communityService.delete(id);
        return LIST_ACTION;
    }
}
